package com.example.myapplication.view;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.example.myapplication.R;
import com.example.myapplication.utils.LogUtils;

import java.util.List;

public class CustomNumKayUtils implements CustomNumKeyView.CallBack{
    private static final CustomNumKayUtils customNumKayUtils = new CustomNumKayUtils();
    private CustomNumKeyView mCustomKeyView;
    private PopupWindow mPop;
    private View mPopView;

    private View my_root;
    private EditText et_view;

    public static CustomNumKayUtils init(){
        return customNumKayUtils;
    }

    public void initKeyBoad(EditText met_view, final Context mContext, View m_root) {
        et_view = met_view;
        my_root = m_root;
        // 自己监听EditText的点击事件弹出我们自定义的键盘
        et_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                // 设置不弹出系统键盘
                et_view.setInputType(InputType.TYPE_NULL);
                et_view.setFocusable(true);
                et_view.setFocusableInTouchMode(true);
                et_view.requestFocus();
                InputMethodManager imm = (InputMethodManager)
                        mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(my_root.getWindowToken(), 0);
                mPop.showAtLocation(my_root, Gravity.BOTTOM, 0, 0);
                return false;
            }
        });
        mPop = new PopupWindow();
        mPopView = LayoutInflater.from(mContext).inflate(R.layout.custom_keyboardview, null);
        mPop.setContentView(mPopView);
        mPop.setTouchable(true);
        mPop.setFocusable(true);
        mPop.setBackgroundDrawable(new ColorDrawable());
        mPop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mCustomKeyView = mPopView.findViewById(R.id.keyboardview);
        // 设置回调，并进行文本的插入与删除
        mCustomKeyView.setOnCallBack(this);
        mCustomKeyView.setRandomKeyBoard(true);
    }

    public void show(Context mContext){
        if(mContext==null||et_view==null||mPop==null||my_root==null){
            return;
        }
        // 设置不弹出系统键盘
        et_view.setInputType(InputType.TYPE_NULL);
        et_view.setFocusable(true);
        et_view.setFocusableInTouchMode(true);
        et_view.requestFocus();
        InputMethodManager imm = (InputMethodManager)
                mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(my_root.getWindowToken(), 0);
        mPop.showAtLocation(my_root, Gravity.BOTTOM, 0, 0);
    }

    public static boolean isActivityRunning(Context mContext, String activityClassName){
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> info = activityManager.getRunningTasks(1);
        if(info != null && info.size() > 0){
            ComponentName component = info.get(0).topActivity;
            LogUtils.i("dove",activityClassName+"---name---"+component.getClassName());
            if(activityClassName.equals(component.getClassName())){
                return true;
            }
        }
        return false;
    }


    public void hide(){
        if(mPop!=null){
            mPop.dismiss();
        }
    }

    @Override
    public void clickNum(String num) {
        if (et_view.getText().length() < 6) {
            et_view.append(num);
            //文本长度为6时隐藏键盘
            if (et_view.getText().length() == 6) {
                hide();
            }
        }
    }

    @Override
    public void deleteNum() {
        int last = et_view.getText().length();
        if (last > 0) {
            //删除最后一位
            et_view.getText().delete(last - 1, last);
        }
    }
}
